package com.webull.information.center.carwler.common.util.jsoup;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author shimingjun
 * @date 2017年2月5日 下午5:54:24
 * @version 1.0
 * @since JDK 1.8
 */
public class SelectorDemo {
	public static void main(String[] args) throws IOException {
		String url = "https://www.google.com/finance?q={0}&ei=tEaEWNHoDMLs0gT-7avoDg";
		List<String> strs = FileUtils.readLines(new File("C:/Users/Administrator/Documents/编辑1.txt"), "utf-8");

		String[] rightExchages = new String[] { "NSE", "BOM" };
		for (String str : strs) {
			// str = "Twilight & Litaka Pharma Ltd";
			Document doc = Jsoup.connect(StringUtils.replaceOnce(url, "{0}", URLEncoder.encode(str, "UTF-8"))).timeout(100000).get();
			StringBuilder result = new StringBuilder("");
			Optional.ofNullable(doc.select("table.gf-table>tbody>tr.snippet")).ifPresent(v -> {

				List<Pair> paris = new ArrayList<>();
				int[] diffLow = new int[] { 99 };// 名称最小差异

				v.forEach(v0 -> {
					Elements es = v0.getElementsByTag("td");
					Pair pp = new Pair();
					if (es != null && !es.isEmpty()) for (Element et : es) {
						String value = StringUtils.strip(et.text());
						if (et.hasClass("symbol"))
							pp.setSymbol(value);
						else if (et.hasClass("exch"))
							pp.setExchange(value);
						else if (et.hasClass("localName")) pp.setCompanyName(value);
					}

					if (ArrayUtils.contains(rightExchages, pp.getExchange())) {
						paris.add(pp);
						pp.setDiff(differenceStr(str, pp.getCompanyName()));
						diffLow[0] = NumberUtils.min(diffLow[0], pp.getDiff());
					}
				});
				System.out.print("name_diff：" + diffLow[0] + "\t");
				paris.sort((a1, a2) -> {
					int a = ArrayUtils.indexOf(rightExchages, a1.getExchange());
					int b = ArrayUtils.indexOf(rightExchages, a2.getExchange());
					return a - b;
				});
				// 取差异最小的
				for (Pair pp : paris) {
					if (pp.getDiff() == diffLow[0]) result.append(pp.toString());
				}

			});
			System.out.println(str + "\t" + result.toString());
		}
	}

	/**
	 * 判断公司名称差异性
	 * 
	 * @param a1
	 * @param a2
	 * @return
	 * @since 0.1.0
	 */
	private static int differenceStr(String a1, String a2) {
		char[] c1 = StringUtils.deleteWhitespace(a1.toLowerCase()).toCharArray();
		char[] c2 = StringUtils.deleteWhitespace(a2.toLowerCase()).toCharArray();
		return ArrayUtils.removeElements(c2, c1).length;
	}

	private static class Pair {
		private String exchange;
		private String symbol;
		private String companyName;
		private int diff;

		public int getDiff() {
			return diff;
		}

		public void setDiff(int diff) {
			this.diff = diff;
		}

		public String getExchange() {
			return exchange;
		}

		public void setExchange(String exchange) {
			this.exchange = exchange;
		}

		public String getSymbol() {
			return symbol;
		}

		public void setSymbol(String symbol) {
			this.symbol = symbol;
		}

		public String getCompanyName() {
			return companyName;
		}

		public void setCompanyName(String companyName) {
			this.companyName = companyName;
		}

		@Override
		public String toString() {
			return symbol + "\t" + exchange + "\t";
		}

	}

}
